var Topic = require('../../models/test/topic')
var Practice = require('../../models/test/practice')
var Result = require('../../models/test/result')

var Test = {
    topic: Topic,
    practice: Practice,
    result: Result
}

var tabs = ['topic', 'practice', 'result']

module.exports = {
    getList: function (req, res) {
        var type = req.params.type
        var skip = + req.params.skip
        var limit = + req.params.limit
        var filter = req.body

        Test[tabs[type]].find(filter).sort({createdAt: -1}).skip(skip).limit(limit).exec(function (err, tests) {
            if (err) {
                console.log('testController getList fail. err = ' + err)
                return res.json({code: 177, msg: '获取失败'})
            }
            if (!tests.length) {
                return res.json({code: 112, msg: '没有习题了'})
            }
            return res.json({code: 100, data: {list: tests}})
        })

    },
    create: function (req, res) {
        var type = req.params.type
        var body = req.body

        new Test[tabs[type]](body).save(function (err, test) {
            if (err) {
                console.log('testController create fail. err = ' + err)
                return res.json({code: 177, msg: '创建失败'})
            }
            return res.json({code: 100, data: {test: test}})
        })
    },
    getOne: function (req, res) {
        var type = req.params.type
        var id = req.params.id

        Test[tabs[type]].findOne({_id: id}, function (err, test) {
            if (err) {
                console.log('testController getOne fail. err = ' + err)
                return res.json({code: 177, msg: '获取失败'})
            }
            return res.json({code: 100, data: {test: test}})
        })
    },
    getOneByTitle: function (req, res) {
        var type = req.params.type
        var title = req.params.title

        Test[tabs[type]].findOne({title: title}, function (err, test) {
            if (err) {
                console.log('testController getOneByTitle fail. err = ' + err)
                return res.json({code: 177, msg: '获取失败'})
            }
            return res.json({code: 100, data: {test: test}})
        })
    },
    update: function (req, res) {
        var body = req.body
        var type = req.params.type
        var id = req.params.id

        Test[tabs[type]].findOneAndUpdate({_id: id}, body, {upsert: true, new: true}, function (err, test) {
            if (err) {
                console.log('testController update fail. err = ' + err)
                return res.json({code: 177, msg: '更新失败'})
            }
            return res.json({code: 100, data: {test: test}})
        })
    },
    //  删除习题，当前只提供练习题删除 
    removeTopic: function (req, res) {
        var id = req.params.id

        Topic.remove({_id: id}, function (err) {
            if (err) {
                console.log('testController removeTopic fail. err = ' + err)
                return res.json({code: 177, msg: '删除练习题'})
            }
            Result.remove({topicId: id}, function (err) {
                if (err) {
                    console.log('testController removeTopic fail. err = ' + err)
                    return res.json({code: 177, msg: '删除练习题'})
                }
                return res.json({code: 100, msg: '删除成功'})
            })
        })

    }

}